<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>按钮控件</title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="leipi.style.css">
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <script src="../../../../jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        /* Thank you by
        http://www.alt-tag.com/blog/2006/02/ie-dom-bugs/ */
        function createElement(type, name) {
            var element = null;
            try {
                element = document.createElement('<' + type + ' name="' + name + '">');
            } catch (e) {
            }
            if (element == null) {
                element = document.createElement(type);
                element.name = name;
            }
            return element;
        }

        function getContextPath() {
            var pathName = document.location.pathname;
            var index = pathName.substr(1).indexOf("/");
            var result = pathName.substr(0, index + 1);
            return result;
        }

    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
        <tr>
            <th>控件字段&nbsp;<span class="label label-important">*</span></th>
            <th><span>控件名称&nbsp;</span><span class="label label-important">*</span></th>
        </tr>
        <tr>
            <th><input type="text" id="orgname" placeholder="必填项"></th>
            <th><input type="text" id="orgtitle" placeholder="必填项"></th>
        </tr>
        <tr>
            <th><span>脚本</span></th>
            <th>提示</th>
        </tr>
        <tr>
            <td>
                <textarea cols="" id="orgScript" name="orgScript" style="height:100px"></textarea>
                <div><input type="button" value="编辑" onclick="editProps()"/></div>
            </td>
            <td>
                <textarea id="tip" name="tip" style="width:250px; height:100px"></textarea>
                <input id="canNull" name="canNull" type="checkbox" value="1" checked style="display:none"/>
            </td>
        </tr>
        <tr style="display:none">
            <th></th>
            <th><span>长度/大小</span></th>
        </tr>
        <tr style="display:none">
            <td>

            </td>
            <td>
                <select id="minT" name="minT" style="width:60px">
                    <option value="d=">>=</option>
                    <option value="d">></option>
                    <option value="=">=</option>
                </select>
                <input id="minV" name="minV" type="text" style="width:40px">
                <select id="maxT" name="maxT" style="width:60px">
                    <option value="x="><=</option>
                    <option value="x"><</option>
                </select>
                <input id="maxV" name="maxV" type="text" style="width:40px">

                <select id="orgalign" style="display:none">
                    <option value="left">左对齐</option>
                    <option value="center">居中对齐</option>
                    <option value="right">右对齐</option>
                </select>
            </td>
        </tr>
        <tr>
            <th><span>宽度</span></th>
            <th><span style="display:none">可见性</span></th>
        </tr>
        <tr>
            <td>
                <input id="orgwidth" type="text" value="" class="input-small span1" placeholder="auto"/>
                px
                <span style="display: none;">
                <input id="orgheight" type="text" value="" class="input-small span1" placeholder="auto"/>
                &
                <input id="orgfontsize" type="text" value="" class="input-small span1" placeholder="auto"/> px
                </span>
            </td>
            <td>
                <label style="display:none" class="checkbox inline"><input id="orghide" type="checkbox"/> 隐藏 </label>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var oNode = null, thePlugins = 'button';
    window.onload = function () {
        if (UE.plugins[thePlugins].editdom) {
            oNode = UE.plugins[thePlugins].editdom;

            var gValue = oNode.getAttribute('value');
            var gScript = oNode.getAttribute('script');
            if (gValue)
                gValue = gValue.replace(/&quot;/g, "\"");
            var gName = oNode.getAttribute('name').replace(/&quot;/g, "\"");
            var gTitle = oNode.getAttribute('title').replace(/&quot;/g, "\"");
            var gHidden = oNode.getAttribute('orghide');
            var gFontSize = oNode.getAttribute('orgfontsize');
            var gAlign = oNode.getAttribute('orgalign');
            var gWidth = oNode.getAttribute('orgwidth');
            var gHeight = oNode.getAttribute('orgheight');

            var gCanNull = oNode.getAttribute("canNull");
            var gMinT = oNode.getAttribute("minT");
            var gMinV = oNode.getAttribute("minV");
            var gMaxT = oNode.getAttribute("maxT");
            var gMaxV = oNode.getAttribute("maxV");
            var gkind = oNode.getAttribute("kind");
            var gTip = oNode.getAttribute("tip");
            if (gTip == null) {
                gTip = '';
            }

            gValue = gValue == null ? '' : gValue;
            gTitle = gTitle == null ? '' : gTitle;
            $G('orgname').value = gName;
            $G('orgtitle').value = gTitle;
            $G('orgScript').value = gScript;
            if (gHidden == '1') {
                $G('orghide').checked = true;
            }
            $G('orgfontsize').value = gFontSize;
            $G('orgwidth').value = gWidth;
            $G('orgheight').value = gHeight;
            $G('orgalign').value = gAlign;
            $G('tip').value = gTip;
        }
    }
    dialog.oncancel = function () {
        if (UE.plugins[thePlugins].editdom) {
            delete UE.plugins[thePlugins].editdom;
        }
    };
    dialog.onok = function () {
        var gName = $G('orgname').value.replace(/\"/g, "&quot;");
        if (gName == '') {
            alert('请输入控件字段');
            return false;
        }
        var gTitle = $G('orgtitle').value.replace(/\"/g, "&quot;");
        if (gTitle == '') {
            alert('请输入控件名称');
            return false;
        }

        var gFontSize = $G('orgfontsize').value;
        var gAlign = $G('orgalign').value;
        var gWidth = $G('orgwidth').value;
        var gHeight = $G('orgheight').value;

        var gCanNull = $G('canNull').checked ? 1 : 0;
        var gMinT = $G('minT').value;
        var gMinV = $G('minV').value;
        var gMaxT = $G('maxT').value;
        var gMaxV = $G('maxV').value;
        var gScript = $G('orgScript').value;
        var gTip = $G('tip').value;

        if (!oNode) {
            try {
                oNode = createElement('input', gName);
                oNode.setAttribute('title', gTitle);
                oNode.setAttribute('value', gTitle);
                oNode.setAttribute('cwsPlugins', thePlugins);
                if ($G('orghide').checked) {
                    oNode.setAttribute('orghide', 1);
                } else {
                    oNode.setAttribute('orghide', 0);
                }
                if (gFontSize != '') {
                    //style += 'font-size:' + gFontSize + 'px;';
                    oNode.setAttribute('orgfontsize', gFontSize);
                }
                if (gAlign != '') {
                    //style += 'text-align:' + gAlign + ';';
                    oNode.setAttribute('orgalign', gAlign);
                }
                if (gWidth != '') {
                    oNode.style.width = gWidth + gUnit;
                    //style += 'width:' + gWidth + 'px;';
                    oNode.setAttribute('orgwidth', gWidth + gUnit);
                }
                if (gHeight != '') {
                    oNode.style.height = gHeight + 'px';
                    //style += 'height:' + gHeight + 'px;';
                    oNode.setAttribute('orgheight', gHeight);
                }

                oNode.setAttribute("canNull", gCanNull);
                oNode.setAttribute("minT", gMinT);
                oNode.setAttribute("minV", gMinV);
                oNode.setAttribute("maxT", gMaxT);
                oNode.setAttribute("maxV", gMaxV);
                oNode.setAttribute("type", "button");
                oNode.setAttribute("kind", "BUTTON");
                oNode.setAttribute("fieldtype", "0");
                oNode.setAttribute("script", gScript);
                oNode.setAttribute("tip", gTip);
                oNode.setAttribute("class", "btn btn-default");

                editor.execCommand('insertHtml', oNode.outerHTML);
            } catch (e) {
                try {
                    editor.execCommand('error');
                } catch (e) {
                    alert('控件异常！');
                }
                return false;
            }
        } else {
            oNode.setAttribute('name', gName);
            oNode.setAttribute('title', gTitle);
            //oNode.setAttribute('value', $G('orgvalue').value);
            if ($G('orghide').checked) {
                oNode.setAttribute('orghide', 1);
            } else {
                oNode.setAttribute('orghide', 0);
            }
            if (gFontSize != '') {
                oNode.style.fontSize = gFontSize + 'px';
                oNode.setAttribute('orgfontsize', gFontSize);
            } else {
                oNode.style.fontSize = '';
                oNode.setAttribute('orgfontsize', '');
            }
            if (gAlign != '') {
                oNode.setAttribute('orgalign', gAlign);
            } else {
                oNode.setAttribute('orgalign', '');
            }
            if (gWidth != '') {
                oNode.style.width = gWidth + gUnit;
                oNode.setAttribute('orgwidth', gWidth + gUnit);
            } else {
                oNode.style.width = '';
                oNode.setAttribute('orgwidth', '');
            }
            if (gHeight != '') {
                oNode.style.height = gHeight + 'px';
                oNode.setAttribute('orgheight', gHeight);
            } else {
                oNode.style.height = '';
                oNode.setAttribute('orgheight', '');
            }

            oNode.setAttribute("value", gTitle);
            oNode.setAttribute("script", gScript);
            oNode.setAttribute("tip", gTip);

            delete UE.plugins[thePlugins].editdom;
        }
    };

    function setButtonScript(script) {
        $G('orgScript').value = script;
    }

    function editProps() {
        console.log('editProps');
        var val = $G('orgScript').value;
        if (!val) {
            val = '{}';
        }
        const json = $.parseJSON(val);
        window.parent.handleEditButtonProps(json);
    }

    // 对字符串中的引号进行解码
    function decodeJSON(jsonString) {
        jsonString = jsonString.replace(/%eq/gi, "=");
        jsonString = jsonString.replace(/%lb/gi, "{");
        jsonString = jsonString.replace(/%rb/gi, "}");
        jsonString = jsonString.replace(/%co/gi, ","); // 逗号

        jsonString = jsonString.replace(/%dq/gi, '"');
        jsonString = jsonString.replace(/%sq/gi, "'");

        // 不能解码回车换行，否则会导致过滤条件为脚本型时，如果有回车换行，会导致点击编辑按钮时，eval报错
        // jsonString = jsonString.replace(/%rn/g, "\r\n")
        // jsonString = jsonString.replace(/%n/g, "\n")
        return jsonString;
    }
</script>
</body>
</html>